package com.cseg674.datamanagement.adler;

import com.hp.hpl.jena.db.*;

import java.sql.*;

/** A container for the configuration properties used to connect to the database.
 *
 */

public class DatabaseConnection
{
	/** Class constructor
	 * 
	 * @param databasePath	URL used to connect to the database
	 * @param projectName	Name of the Java project to connect to
	 * @param userName		Name of the user used to connect to the database
	 * @param password		Password of the user used to connect to the database
	 * @param create		NULL
	 * @param ontology		The ontology
	 */
    public DatabaseConnection(String databasePath, String projectName,
        String userName, String password, String create, String ontology)
    {
        this.databasePath   = databasePath;
        this.projectName    = projectName;
        this.userName       = userName;
        this.password       = password;
        this.ontology       = ontology;
        
        dbConnection = null;
    }

    /** Initializes the connection to the vendor database
     * 
     */

    /// see DBModel interface for comments
    public void initialize() throws AdlerException
    {

        String jdbcDriver = "";
        String dbType     = "";

        // connect to MySQL DB
        if(databasePath.startsWith("mysql")) {
            dbType     = "MySQL";
            jdbcDriver = "com.mysql.jdbc.Driver";
        }
        // connect to MsSQL DB
        else if(databasePath.startsWith("sqlserver")) {
            dbType     = "MsSQL";
            jdbcDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        }
        else {
            throw new AdlerException("Unsupported sql type: " + databasePath);
        }

        // build full java database url
        String dbURL = "jdbc:" + databasePath;

        // attempt to load jdbc driver
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch(ClassNotFoundException e) {
            throw new AdlerException("Error loading JDBC driver");
        }

        // create new jdbc connection
        dbConnection = new DBConnection(dbURL, userName, password, dbType);
    }
    
    /** Closes the connection to the vendor database
     * 
     */
    
    /// see DBModel interface for comments
    public void shutdown()
    {
        try {
            dbConnection.close();    
        } catch(SQLException e) {}
    }
    
    /** Returns the name of the default Java project
     * 
     * @return The name of the project
     */

    public String getDefaultProjectName()
    {
        return projectName;
    }
    
    /** Returns the ontology 
     * 
     * @return The ontology
     */

    public String getOntology()
    {
        return ontology;
    }

    /** Returns the current connection to the vendor database
     * 
     * @return The connection to the vendor database
     */
    
    public IDBConnection getConnection() {
        return dbConnection;
    }
    
    private IDBConnection dbConnection;

    private String  databasePath;
    private String  projectName;
    private String  userName;
    private String  password;
    private String  ontology;
}